package com.swfee.qinben;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ListView;

import com.swfee.qinben.base.AppBaseActivity;
import com.swfee.qinben.dto.active.LinkerModel;
import com.swfee.qinben.dto.rsp.ContactRspDto;
import com.swfee.qinben.httpactions.ContactAction;
import com.swfee.qinben.util.CurenActivitUtils;

/**
 * 我的联系人
 * @author ddr
 *
 */
public class T2Activity extends AppBaseActivity {

	private List<LinkerModel> linkerModels = new ArrayList<LinkerModel>();
	private LayoutInflater inflater;
	private int focusLastIndex = -1;
	private OnOperatorClick onOperatorClick =new OnOperatorClick();
	private Handler handle = new Handler() {

		public void handleMessage(Message msg) {

		};
	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		CurenActivitUtils.setHeaderTitle("我的联系人", 1);
		inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		setContentView(R.layout.index_linkers);

		asynInitData();
	}

	public void asynInitData() {

		showProgressDialog("加载联系人数据，请稍候..");
		new Thread(new Runnable() {

			@Override
			public void run() {

				final List<ContactRspDto> rspDtos = new ContactAction()
						.getContacts();
				if (rspDtos != null) {
					for (int i = 0; i < rspDtos.size(); i++) {
						ContactRspDto dto = rspDtos.get(i);
						linkerModels.add(new LinkerModel(dto.getUserName(), dto
								.getComment(), dto.getPhoneNumber()));
					}
				}

				handle.post(new Runnable() {

					@Override
					public void run() {
						hideProgressDialog();
						ListView listView = (ListView) findViewById(R.id.listView);
						listView.setOnItemClickListener(new OnItemClickListener() {

							@Override
							public void onItemClick(AdapterView<?> v1, View v2,
									int ind, long arg3) {

								if (focusLastIndex == ind) {
									return;
								}

								if (focusLastIndex != -1) {
									View v = v1.getChildAt(focusLastIndex).findViewById(R.id.linker_opt_lay);
									v.setVisibility(View.GONE);
								}
								LinearLayout ll = (LinearLayout) v2	.findViewById(R.id.linker_opt_lay);
								ll.setVisibility(View.VISIBLE);
								focusLastIndex = ind;

							}
						});

						listView.setAdapter(new LinkerAdapter());
					}
				});

			}
		}).start();

	}

	// 适配器
	private class LinkerAdapter extends BaseAdapter {
		@Override
		public int getCount() {
			return linkerModels.size();
		}

		@Override
		public Object getItem(int position) {
			return linkerModels.get(position);
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			if (convertView == null) {
				convertView = inflater.inflate(R.layout.linker_include, null);
				LinkerModel model =  linkerModels.get(position);
				CurenActivitUtils.setTextViewContent(R.id.full_name, model.getFullName(), convertView);
				CurenActivitUtils.setTextViewContent(R.id.last_name, model.getLastName(), convertView);
				CurenActivitUtils.setTextViewContent(R.id.mobileShow, model.getMobileShow(), convertView);
				CurenActivitUtils.setTextViewContent(R.id.address, model.getAddress(), convertView);
				Button btnCallThis = (Button) convertView.findViewById(R.id.btn_call_this);
				Button btnLeaveMsg = (Button) convertView.findViewById(R.id.btn_leave_msg);
				Button btnAppointment = (Button) convertView.findViewById(R.id.btn_appointment);
				btnCallThis.setOnClickListener(onOperatorClick);
				btnLeaveMsg.setOnClickListener(onOperatorClick);
				btnAppointment.setOnClickListener(onOperatorClick);
				
			}

			return convertView;
		}

	}
	
	
	private class OnOperatorClick implements View.OnClickListener{
		@Override
		public void onClick(View v) {
			
				LinkerModel curMode = linkerModels.get(focusLastIndex);
				switch (v.getId()) {
				case R.id.btn_call_this :
					{
						Intent phoneIntent = new Intent(Intent.ACTION_DIAL,
	
								Uri.parse("tel:"+ curMode.getMobile()));
								// 启动
								startActivityUndoFinish(phoneIntent);
					}
					break;
				case R.id.btn_leave_msg :
								
								break;
			
				case R.id.btn_appointment :
					
					break;


				default:
					break;
				}
			
			
		}
	}
	
	

}
